/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.activities;

import gr.ebs.gssdroid.GssDroid;
import gr.ebs.gssdroid.GssDroidApplication;
import gr.ebs.gssdroid.http.GssHttpException;
import gr.ebs.gssdroid.http.SystemErrorException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

public abstract class AbstractGssActivity extends Activity{
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	        onBackPressed();
	        return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}
	
	public void onBackPressed() {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Exit");
		alertDialog.setMessage("R u sure you want to exit GssDroid?");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(getParent() instanceof GssDroid)
					getParent().finish();
				else
					finish();
			}
		});
		alertDialog.setButton2("CANCEL", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		alertDialog.setCancelable(true);
		alertDialog.show();

	}
	
	protected void showErrorDialog(String title, String message){
		AlertDialog alertDialogn = new AlertDialog.Builder(this).create();
		alertDialogn.setTitle(title);
		alertDialogn.setMessage(message);
		alertDialogn.setButton("Continue",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		alertDialogn.setCancelable(true);
		alertDialogn.show();
	}
	
	protected void handleRestException(String type, Throwable e) {
		String errorMessage = "";
		Log.e("UPLOAD","error",e);
		if (e instanceof SystemErrorException){
			Log.e("UPLOAD","error",e);
			errorMessage = e.getMessage();
		}
		else if (e instanceof GssHttpException)
			if (((GssHttpException) e).getStatusCode() > 0)
				errorMessage = "Status Code:"
						+ ((GssHttpException) e).getStatusCode() + "\n"
						+ e.getMessage();
		AlertDialog alertDialogn = new AlertDialog.Builder(this).create();
		alertDialogn.setTitle("An error occured");
		alertDialogn.setMessage(errorMessage);
		alertDialogn.setButton("Continue",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		alertDialogn.setCancelable(true);
		alertDialogn.show();

	}
	
	
	protected void handleLogin() {
		Intent intent = new Intent(getApplication(), GssLogin.class);
		startActivity(intent);
		// close out this activity
		finish();		
	}
	protected void handleSettings() {
		Intent intent = new Intent(getApplication(), GssSettings.class);
		startActivity(intent);
		// close out this activity
		finish();		
	}
	
	public GssDroidApplication getDroidApplication(){
		return (GssDroidApplication) getApplicationContext();
	}

}
